const app = getApp()
var util = require('../../utils/util')
var base = new util.Base64()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    host: wx.$.host,
    navH: app.data.navHeight,
    userInfo: wx.getStorageSync('vs-info'),
    portrait:'https://vstartup-1253985024.cos.ap-beijing.myqcloud.com/zxstest/images/card/200729/18cbf1a9f26027294b90ecb0854f025108b90320.jpeg',
    current:0,
    loading:true,
    banner:[],
    roomSetting:{},
    /* 弹框 */ 
    isShowCertificate: true,
    isEwmShow: true,  // 二维码弹窗
    ewmSrc: '',   // 二维码base64url
    cardInfoData: null,   // 当前二维码卡片信息
    memberSignin: '',  // 状态码
    memberSigninErrorMsg: '',
    cardsdata:[],   // 卡列表数据
    selectCardId: '',  // 选中的卡片id
    cardsdataindex:'',
    sharefriend:false,
    /* 弹框 --end */
    onlogin: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   onlogin: wx.getStorageSync('vs-token')
    // })
    if (wx.getStorageSync('vs-token')) {
      this.setData({
        onlogin: 1
      })
    } else {
      this.setData({
        onlogin: 2
      })
    }
    this.getbanner()
    this.getRoomSetting();
    let left = this
    if (app.data.jsonPage) {
      let nvabarData = {
        title: app.data.jsonPage.home.title,
        showCapsule: false,
        textclase: false,
        texttype: false,
      }
      this.setData({
        pageToconfigure: app.data.jsonPage.home,
        nvabarData: nvabarData,
      })
    } else {
      app.getjsonPage = res => {
        let nvabarData = {
          title: res.home.title,
          showCapsule: false,
          textclase: false,
          texttype: false,
        }
        left.setData({
          pageToconfigure: res.home,
          nvabarData: nvabarData,
        })
      }
    }
    if (wx.getStorageSync('vs-token') && wx.getStorageSync('vs-info')) {
      if (options.recommend_id) {
        left.setData({
          isShowCertificate:false,
          sharefriend: true,
        })
      } else if (options.channel){
        wx.navigateTo({
          url: "/pages/openingVip/openingVip"
        });
      }
    } else {
      if (options.recommend_id){
        app.btnLandingJudgment('',false,true)
        app.AcceptData = res=>{
          left.setData({
            isShowCertificate: false,
            sharefriend: true,
          })
        }
      } else if (options.channel) {
        app.btnLandingJudgment('', false, true)
        app.AcceptData = res => {
          wx.navigateTo({
            url: "/pages/openingVip/openingVip"
          });
        }
      }
    }
  },
  // 阻止弹窗下页面滚动
  myCatchTouch() {
    return
  },
  // 跳转购买卡列表页面
  Jumpinfo () {
    wx.navigateTo({
      url: "/pages/openingVip/openingVip"
    });
  },
  /* 页面文字 */ 
  getRoomSetting(){
    wx.$.fetch('api/room-setting',{
        hideLoading:false,
        method:'get'
    }).then(res=>{
       this.setData({
         roomSetting:res.data.data
       })
    })
  },
  map () {
    wx.getLocation({//获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: 39.98207640159584,//要去的纬度-地址
          longitude: 116.31138497858932,//要去的经度-地址
          name: "北京市海淀区中关村大街15号",
          address:'北京市海淀区中关村大街15号'
        })
      }
    })
  },
  /* 请求banners图数据 */
  getbanner(){
    wx.$.fetch('api/banners', {
      hideLoading: false,
      method: 'get',
      data: {
        type: 'banner'
      }
    }).then(res => {
      res = res.data;
      this.setData({
        banner: res.data
      })
    })
  },
  /* 扫描二维码 */
  Sweepcodejudge() {
    app.btnLandingJudgment('', false, true)
    app.AcceptData = res => {
      this.Sweepcode()
    }
  },
  // 签到（生成二维码）
  Sweepcode() {
    let left = this
    left.getMemberSignin()
    // wx.scanCode({
    //   success(res) {
    //     let path = res.path
    //       ? res.path
    //       : decodeURIComponent(base.decode(res.rawData));
    //     let data = util.getQueryObject(path);
    //     if (data.Signintype){
    //       left.getMemberSignin()
    //     } else {
    //       wx.showToast({
    //         title: '请扫签到二维码',
    //         icon: 'none',
    //         mask: true,
    //         duration: 5000,
    //       })
    //     }
    //   },
    //   fail(res) {
    //     wx.showToast({
    //       title: '扫码失败请重新扫码',
    //       icon: 'none',
    //       mask: true,
    //       duration: 4000,
    //     })
    //   },
    // })
  },
  /* 签到 */
  getMemberSignin(){
    let left = this;
    const member_card_id = this.data.selectCardId ? this.data.selectCardId : '';
    // wx.$.fetch(`api/member/signin`, {
    //   method: 'POST',
    //   data: {
    //     type: 1,
    //     member_card_id: member_card_id
    //   },
    // }).then(res => {
    //   if (res.data.code == 200) {
    //     left.setData({
    //       isShowCertificate: false,
    //       memberSignin: res.data.code
    //     })
    //     left.data.cardsdataindex = '';
    //   } else if (res.data.code == 30007) {
    //     left.setData({
    //       isShowCertificate: false,
    //       memberSignin: res.data.code
    //     })
    //   } else if (res.data.code == 30006) {
    //     left.setData({
    //       isShowCertificate: false,
    //       memberSignin: res.data.code,
    //       cardsdata: res.data.data.cards,
    //       cardsdataindex: res.data.data.cards[0].id
    //     })
    //   } else {
    //     wx.showToast({
    //       title: res.data.data.message,
    //       icon: 'none',
    //       mask: true,
    //       duration: 4000,
    //     })
    //   }
    // })
    wx.$.fetch('api/member/card/availableCardQrcode',{
      method: 'GET',
      data: {
        member_card_id: member_card_id
      }
    }).then(res => {
      if (res.data.code == 200) {
        left.setData({
          isShowCertificate: false,
          memberSignin: res.data.code,
          isEwmShow: false,
          ewmSrc: res.data.data.qrcode,
          cardInfoData: res.data.data
        })
        left.data.selectCardId = '';
      } else if (res.data.code == 30006) {
        left.setData({
          isShowCertificate: false,
          memberSignin: res.data.code,
          cardsdata: res.data.data.cards,
          isEwmShow: true,
          ewmSrc: '',
          cardsdataindex: res.data.data.cards[0].id,
          selectCardId: res.data.data.cards[0].id
        })
      } else {
            left.setData({
              isShowCertificate: false,
              memberSigninErrorMsg: res.data.msg,
              memberSignin: res.data.code,
            })
      }
      // } else {
      //   wx.showToast({
      //     title: res.data.data.msg,
      //     icon: 'none',
      //     mask: true,
      //     duration: 4000,
      //   })
      // }
    })
  },
  // 选择未激活的卡片
  selectCurCard(selectItem) {
    this.setData({
      selectCardId: selectItem.currentTarget.dataset.id
    })
  },
  /* banner图 */
  getSwiperitembanner(current) { 
    this.setData({
      current:current.detail.current
    })
  },
  /* wxParse回调 */
  handleWxparseRendered: function () {
    setTimeout(() => {
      wx.hideLoading();
    }, 200);
    this.setData({
      loading: false
    });
  },
 /* 跳转手机号授权页面 */
  getbindTel(){
    wx.navigateTo({
      url: '/pages/bindTel/bindTel',
      success: function (res) {
        res.eventChannel.emit('TransferOfData2', {
          url: '',
          typeurl: false,
          executeType: false,
        })
      },
    })
  },
  /* 开启自习室或购买卡 */
  btnUse(){
    wx.$.fetch('api/member/has-available-card',{
      hideLoading: false,
      method: 'get',
    }).then(res=>{
      if(res.data.code == 200){
        if (res.data.data == 1){
          this.Sweepcode()
        }else{
          this.purchaseVip();
        }
      }else{
        wx.showModal({
          title: "提示",
          content: res.data.msg,
          confirmColor: "#50B23F",
          showCancel: false
        });
      }
    })
  },
  /* 选中得卡 */ 
  getSwiperitem(current) {
    let index = current.detail.current;
    this.data.cardsdataindex = this.data.cardsdata[index].id;
  },
  /* 购买vip */
  purchaseVip() {
    this.showCertificatec()
    wx.navigateTo({
      url: "/pages/openingVip/openingVip"
    });
  },
  /* 分享去购买 */
  sharePurchaseVip() {
    wx.navigateTo({
      url: "/pages/openingVip/openingVip"
    });
    this.data.cardsdataindex = '';
    this.setData({
      memberSignin: '',
      sharefriend: false,
      isShowCertificate: true,
    })
  },
  /* 登录 */ 
  getSignin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /* 关闭弹框 */ 
  showCertificatec(){
    wx.$.recommend_id = 0;
    this.data.cardsdataindex = '';
    this.data.selectCardId = '';
    this.setData({
      memberSignin:'',
      sharefriend:false,
      isShowCertificate:true,
      ewmSrc: ''
    })
  },
  /* 跳转我的页面 */
  clickmine() {
    app.btnLandingJudgment("/pages/mine/mine", true, false);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('vs-token')) {
      this.setData({
        onlogin: 1
      })
    } else {
      this.setData({
        onlogin: 2
      })
    }
    if (wx.getStorageSync('vs-token')) {
      var userInfo = wx.getStorageSync('vs-info');
      this.setData({
        portrait: userInfo.avatar,
        userInfo: userInfo,
      })
    } else {
      this.setData({
        portrait: 'https://vstartup-1253985024.cos.ap-beijing.myqcloud.com/zxstest/images/card/200729/18cbf1a9f26027294b90ecb0854f025108b90320.jpeg',
        userInfo: '',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
  /**
   * 页面滚动函数
   */
  onPageScroll: function() {}
})
